<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('商品信息列表')"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list col-sm-12" id="selectType">
                    <ul>
                        <li>
                            <label>选分类：</label>
                            <select name="typeId" class="typeId form-control" onchange="typeChange()">
                                <option value="">全部</option>
                            </select>
                        </li>
                        <li>
                            <label>选品牌：</label>
                            <select name="brandId" class="brandId form-control" onchange="$.table.search()">
                                <option value="">全部</option>
                            </select>
                        </li>
                    </ul>
                </div>
                <div class="select-list col-sm-12" id="selectOther">
                    <ul>
                        <li>
                            <label>找商品：</label>
                            <input type="text" name="productName" onkeyup="$.table.search()"/>
                        </li>
                        <li>
                            <label>拼音找：</label>
                            <input type="text" name="productShortName" onkeyup="$.table.search()"/>
                        </li>
                        <li>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="location.reload()">
                                <i class="fa fa-refresh"></i>&nbsp;全部
                            </a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-warning" onclick="$.table.importExcel()">
                <i class="fa fa-download"></i> 导入
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: jquery-cxselect-js"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<script th:inline="javascript">

    $(function () {
        var options = {
            url: ctx + "baseInfo/product/list",
            createUrl: ctx + "baseInfo/product/add",
            updateUrl: ctx + "baseInfo/product/edit/{id}",
            importUrl: ctx + "baseInfo/product/importData",
            importTemplateUrl: ctx + "baseInfo/product/importTemplate",
            removeUrl: ctx + "baseInfo/product/remove",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            onEditableSave: onEditableSave,
            modalName: "商品信息",
            columns: [{
                checkbox: true
            },
                {
                    field: 'productId',
                    title: '商品id',
                    visible: false
                },
                {
                    field: 'productName',
                    width: '20',
                    widthUnit: '%',
                    title: '商品名'
                },
                {
                    field: 'productBrand',
                    title: '品牌'
                },
                {
                    field: 'productUnit',
                    title: '单位',
                    editable : {
                        type : 'text',
                        title : '单位',
                        emptytext : '个'
                    }
                },
                {
                    field: 'productPrice',
                    title: '单价',
                    editable: {
                        type: 'text',
                        title: '金额',
                        emptytext: '0.00',
                        validate: function (value) {
                            var num = /^[+-]?[0-9]+(\.[0-9]{1,2})?$/;
                            if (!num.test(value)) {
                                return '不符合金额规范，请重新输入';
                            }
                        }
                    }
                },
                {
                    field: 'productType',
                    title: '类型'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    title: '图片',
                    formatter: function(value, row, index) {
                        return $.table.imageView(row.productImg);
                    }
                },
                {
                    field: 'purchasePrice',
                    title: '编号'
                },
                {
                    field: 'productStore',
                    title: '库存'
                },
                {
                    field: 'productShortName',
                    title: '简称'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.productId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.productId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //品牌
    var brandData = [[${brandList}]];
    $('#selectOther').cxSelect({
        required: false,
        firstTitle: '全部',
        selects: ['brandId'],
        jsonName: 'name',
        jsonValue: 'id',
        data: brandData
    });

    //大分类
    var typeParentData = [[${parentType}]];
    $('#selectType').cxSelect({
        required: false,
        firstTitle: '全部',
        selects: ['typeId'],
        jsonName: 'name',
        jsonValue: 'id',
        data: typeParentData
    });

    //小分类选中
    function typeChange() {
        var typeId = $(".typeId").val();
        $.form.reset();
        $.ajax({
            type: "post",
            url: ctx + "baseInfo/product/parentTypeChange",
            data: {
                "typeId": typeId
            },
            success: function (result) {
                if (result.code == 0) {
                    $('#selectType').cxSelect({
                        required: false,
                        firstTitle: '全部',
                        selects: ['brandId'],
                        jsonName: 'name',
                        jsonValue: 'id',
                        data: result.data.brandList
                    });
                    $('#selectType').cxSelect({
                        required: false,
                        firstTitle: '全部',
                        selects: ['sizeId'],
                        jsonName: 'name',
                        jsonValue: 'id',
                        data: result.data.sizeList
                    });
                }
            }
        })
        $(".typeId").val(typeId);
        $.table.search();
    }

    $('#bootstrap-table').on('page-change.bs.table', function (e,number, size) {
        window.scrollTo(0,0);
    });

    //表格编辑保存
    function onEditableSave(field, row, rowIndex, oldValue, $el) {
        $.ajax({
            type: "post",
            url: ctx + "baseInfo/product/edit",
            data: {
                "productId": row.productId,
                "productUnit": row.productUnit,
                "productPrice": row.productPrice
            },
            success: function (result) {
            }
        })
    }

</script>
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在，更新这条数据。"> 是否更新已经存在的用户数据
                &nbsp; <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i
                    class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</html>